package com.watson.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @program: mytest
 * @description:
 * @author: zhangpeng348@jd.com
 * @date: 2019-07-03 21:09
 **/
public class ProducerDemo {
    public final static String EXCHANGE_NAME = "test_exchange_fanout";
    public final static String QUEUE_NAME = "testQueue";

    public static void main(String[] args) throws IOException, TimeoutException {


        Connection connection = null;
        Channel channel = null;
        try {
            connection = RabbitConn.getConnection();
            // 创建一个频道
            channel = connection.createChannel();
//            channel.basicQos(1);
            // 声明默认的队列
            channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
//            channel.queueDeclare(QUEUE_NAME, true, false, true, null);
            long id = 0;
            while (true) {
                channel.basicPublish(EXCHANGE_NAME, "", null, String.valueOf(id++).getBytes());
                Thread.sleep(1000);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (channel != null) {
                channel.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    }

}
